package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class MuriIntermediObject extends JPanel {

	private int count = 0;
	private String[] elPiano = { "", "5°", "4°", "3°", "2°", "1°", "terra",
			"-1°", "-2°", "-3°", "-4°", "-5°", "altro" };
	private JComboBox piano = new JComboBox(elPiano);
	private boolean modPiano = false;
	private JTextField pianoOther = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private String[] divisorio = { " ", "tramezza in laterizio forato",
			"tramezza in laterizio porizzato", "bi-mattoni", "mattone",
			"cartongesso", "legno", "altro" };
	private String[] portante = { " ", "C.A.", "blocchi cemento", "mattoni",
			"bi-mattoni", "poroton", "tufo", "pietra porosa", "sasso",
			"misto sassi e mattoni", "misto pietra e mattoni", "altro" };

	private JComboBox interno = new JComboBox();
	private boolean modInterno = false;
	private JTextField internoOther = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField spessoreInterno = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}

	};

	private MuriIntermediPanel muri;
	private RoundedMuriInterrati muriRound = new RoundedMuriInterrati(
			"Muri intermedi", count, this);

	public MuriIntermediObject(int i, MuriIntermediPanel m) {
		super();
		count = i;
		muri = m;
		setLayout(new MigLayout("", "[]5[]5[]5[]", "[]1[]1[]"));
		setOpaque(false);

		muriRound.setPreferredSize(new Dimension(210, 70));
		muriRound.setOpaque(false);

		setCell();
		addAction();

		add(muriRound, "cell 0 0 1 2");
		add(new JLabel("  PIANO"), "cell 1 0");
		add(piano, "cell 1 1");
		add(interno, "cell 2  1");
		add(spessoreInterno, "cell 3  1");
		add(new JLabel("cm"), "cell 3  1 ");

	}

	public void setMenu(int i) {
		// portante
		remove(interno);
		if (i == 1)
			interno = new JComboBox(portante);
		else
			interno = new JComboBox(divisorio);

		interno.setPreferredSize(new Dimension(250, 35));
		interno.setFont(new Font("Arial", Font.PLAIN, 14));
		add(interno, "cell 2 1");
		addAction();
		paintAll(getGraphics());
	}

	public void remove() {
		muri.removeTabel(count);
	}

	public void setCount(int i) {
		count = i;
	}

	public void setCell() {
		interno.setPreferredSize(new Dimension(250, 35));
		internoOther.setPreferredSize(new Dimension(250, 35));
		piano.setPreferredSize(new Dimension(70, 35));
		pianoOther.setPreferredSize(new Dimension(70, 35));
		spessoreInterno.setPreferredSize(new Dimension(70, 35));

		internoOther.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		pianoOther.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		spessoreInterno
				.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

		internoOther.setOpaque(false);
		pianoOther.setOpaque(false);
		spessoreInterno.setOpaque(false);

		interno.setFont(new Font("Arial", Font.PLAIN, 14));
		internoOther.setFont(new Font("Arial", Font.PLAIN, 14));
		piano.setFont(new Font("Arial", Font.PLAIN, 14));
		pianoOther.setFont(new Font("Arial", Font.PLAIN, 14));
		spessoreInterno.setFont(new Font("Arial", Font.PLAIN, 14));
	}

	public void addAction() {
		// action
		interno.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				if (((String) interno.getSelectedItem())
						.equalsIgnoreCase("altro")) {
					remove(interno);
					add(internoOther, "cell 2 1");
					modInterno = true;
					paintAll(getGraphics());
				}
			}
		});

		piano.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((String) piano.getSelectedItem())
						.equalsIgnoreCase("altro")) {
					remove(piano);
					add(pianoOther, "cell 1 1");
					modPiano = true;
					paintAll(getGraphics());
				}
			}
		});

		// mouse
		MouseAdapter mlA1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(internoOther);
					interno.setSelectedIndex(0);
					add(interno, "cell 2 1");
					modInterno = false;

					paintAll(getGraphics());
				}
			}
		};
		internoOther.addMouseListener(mlA1);

		MouseAdapter mlA2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(pianoOther);
					piano.setSelectedIndex(0);
					add(piano, "cell 1 1");
					modPiano = false;

					paintAll(getGraphics());
				}
			}
		};
		pianoOther.addMouseListener(mlA2);
	}

	public String print() {
		String print = "#" + muriRound.getPiano();
		if (modPiano)
			print = print + "\n#" + pianoOther.getText();

		else
			print = print + "\n#" + piano.getSelectedIndex();

		if (modInterno)
			print = print + "\n#" + internoOther.getText();
		else
			print = print + "\n#" + interno.getSelectedIndex();
		print = print + "\n#" + spessoreInterno.getText();

		return print;
	}

	public void setElement(String data) {
		System.out.println("@@ " + data);
		Scanner c = new Scanner(data);
		// c.nextLine();
		String tmp = c.nextLine();

		try {
			muriRound.setPiano(Integer.parseInt(tmp));

		} catch (NumberFormatException e) {

		}
		tmp = c.nextLine();
		try {
			piano.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			pianoOther.setText(tmp);
			remove(piano);
			add(pianoOther, "cell 1 1");
			modPiano = true;
		}

		tmp = c.nextLine();
		try {
			interno.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			internoOther.setText(tmp);
			remove(interno);
			add(internoOther, "cell 2 1");
			modInterno = true;
		}
		try {
			spessoreInterno.setText(c.nextLine());
		} catch (Exception e) {
			spessoreInterno.setText("");
		}
	}

	public Document pdf(Document doc) {
		PdfPTable table = new PdfPTable(4);
		int[] size = { 60, 25, 90, 30 };
		try {
			table.setWidths(size);
			table.setWidthPercentage(100);
			table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
			table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
			table.getDefaultCell().setFixedHeight(70);
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// muro
		Phrase testo = new Phrase("Muro intermedio " + muriRound.getTesto(),
				FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		PdfPCell cos = new PdfPCell(testo);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);

		// piano
		if (modPiano)
			testo = new Phrase("Piano " + pianoOther.getText(),
					FontFactory.getFont(FontFactory.TIMES, 11));
		else
			testo = new Phrase("Piano " + (String) piano.getSelectedItem(),
					FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setFixedHeight(20);
		table.addCell(cos);

		// interno
		if (modInterno) {
			testo = new Phrase("(interno) " + internoOther.getText(),
					FontFactory.getFont(FontFactory.TIMES, 11));
			cos = new PdfPCell(testo);
			cos.setFixedHeight(20);
		} else {
			if (interno.getSelectedIndex() != 0) {
				testo = new Phrase("(interno) "
						+ (String) interno.getSelectedItem(),
						FontFactory.getFont(FontFactory.TIMES, 11));
				cos = new PdfPCell(testo);
				cos.setFixedHeight(20);
			} else {
				testo = new Phrase("");
				cos = new PdfPCell(testo);
			}
		}
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.disableBorderSide(PdfPCell.RIGHT);
		cos.setFixedHeight(20);
		table.addCell(cos);

		if (!spessoreInterno.getText().equals(""))
			testo = new Phrase(spessoreInterno.getText() + " cm", FontFactory.getFont(
					FontFactory.TIMES, 11));
		else
			testo = new Phrase("");
		cos = new PdfPCell(testo);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.disableBorderSide(PdfPCell.LEFT);
		cos.setFixedHeight(20);
		table.addCell(cos);
		
		// aggiungi tabella
		try {

			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return doc;
	}
}
